package ThreadPool.lagou.concurrent.lock.demo01;

import java.util.concurrent.TimeUnit;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: 手写死锁的代码
 * @date 2022/10/9 13:21
 */
public class DeadLockDemo {

    private static final Object a = new Object();

    private static final Object b = new Object();

    public static void main(String[] args) {

        new Thread(()->{
            synchronized (a){
                System.out.println(Thread.currentThread().getName() + "获得a锁,希望获得b锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (b){
                    System.out.println(Thread.currentThread().getName() + "成功获得b锁");
                }
            }
        },"a").start();

        new Thread(()->{
            synchronized (b){
                System.out.println(Thread.currentThread().getName() + "获得b锁,希望获得a锁");
                synchronized (a){
                    System.out.println(Thread.currentThread().getName() + "成功获得a锁");
                }
            }
        },"b").start();
    }


}
